package Synchronized;

public class DemoWait {

    static class WaitTest implements Runnable{
        private Object locker;

        public WaitTest(Object locker) {
            this.locker = locker;
        }

        @Override
        public void run() {
            synchronized (locker){
                System.out.println("wait开始");
                try {
                    locker.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("wait结束");
            }

        }
    }


    static class NotifyTest implements Runnable{
        private Object locker;

        public NotifyTest(Object locker) {
            this.locker = locker;
        }

        @Override
        public void run() {
            synchronized (locker){
                System.out.println("notify开始");
                //只唤醒一个线程
                //locker.notify();
                //唤醒全部线程
                locker.notifyAll();
                System.out.println("notify结束");
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Object object=new Object();
        Thread thread1=new Thread(new WaitTest(object));
        Thread thread11=new Thread(new WaitTest(object));
        Thread thread12=new Thread(new WaitTest(object));
        Thread thread2=new Thread(new NotifyTest(object));
        thread1.start();
        thread11.start();
        thread12.start();
        Thread.sleep(3000);
        thread2.start();
    }
}
